﻿using System;
using Token.Piker.BLL;
using System.Data;
using Token.Piker.Model;
using Token.Piker.Core;

namespace Token.Piker.Web
{
    public partial class Default : PikerBasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //绑定首页大图
            SlideBLL slideBLL = new SlideBLL();
            rptSlide.DataSource = slideBLL.GetList(4, "SlideState = 1", "AddTime desc");
            rptSlide.DataBind();
            //绑定画题
            ActivityBLL activtyBLL = new ActivityBLL();
            DataSet ds = activtyBLL.GetList(6, string.Empty, "StartTime desc");
            DataSet newds = ds;
            if (ds.Tables[0].Rows.Count > 5)
            {
                ds.Tables[0].Rows.RemoveAt(5);
            }
            if (ds.Tables[0].Rows.Count > 4)
            {
                ds.Tables[0].Rows.RemoveAt(4);
            }
            if (ds.Tables[0].Rows.Count > 3)
            {
                ds.Tables[0].Rows.RemoveAt(3);
            }
            if (newds.Tables[0].Rows.Count > 3)
            {
                newds.Tables[0].Rows.RemoveAt(0);
                newds.Tables[0].Rows.RemoveAt(0);
                newds.Tables[0].Rows.RemoveAt(0);
            }
            else
            {
                newds = null;
            }
            rptFstTopicUl.DataSource = ds;
            rptFstTopicUl.DataBind();
            rptScdTopicUl.DataSource = newds;
            rptScdTopicUl.DataBind();
            //取得新闻图片
            NewsAlbumBLL newsAlbumBLL = new NewsAlbumBLL();
            DataSet newsAlbumds = newsAlbumBLL.GetList(1, "Flag = 0", "CreateTime desc");
            if (newsAlbumds.Tables.Count > 0 && newsAlbumds.Tables[0].Rows.Count > 0)
            {
                anchor_NewsAlbum_ID.HRef = "News_Album.aspx?id=" + newsAlbumds.Tables[0].Rows[0]["ID"].ToString();
                imgNewsAlbum.ImageUrl = newsAlbumds.Tables[0].Rows[0]["CoverURL"].ToString();
                lblThings.Text = newsAlbumds.Tables[0].Rows[0]["AlbumName"].ToString();
                lblPosition.Text = newsAlbumds.Tables[0].Rows[0]["ShotPosition"].ToString();
                lblTime.Text = ((DateTime)newsAlbumds.Tables[0].Rows[0]["ShotTime"]).ToLongDateString();
                //取得图片数量
                int id = int.Parse(newsAlbumds.Tables[0].Rows[0]["ID"].ToString());
                lblPicCount.Text = (new NewsPhotoBLL().GetRecordCount("Album = " + id + "AND Flag =0")).ToString();
            }
            //绑定最近上传
            PhotoBLL photoBLL = new PhotoBLL();
            rptPicBox.DataSource = photoBLL.GetHomePhotoList();
            rptPicBox.DataBind();
        }

        protected string GetSrc(object id, int index)
        {
            int intID = Convert.ToInt32(id);
            Photo photo = (new PhotoBLL()).GetModel(intID);
            int width;
            int height;
            if (index == 0)
            {
                width = 190;
                height = 190;
                //默认显示第一张图片的信息
                ShowFirstImageInfo(photo);
            }
            else
            {
                width = 90;
                height = 90;
            }

            string src = PhotoBLL.GetThumbnailSrc(photo, width, height);
            src = ResolveClientUrl(src);
            return src;
        }

        /// <summary>
        /// 根据是单张还是组图生成不同的链接
        /// </summary>
        /// <param name="id">图片的Id</param>
        /// <param name="albumID">所属相册的ID</param>
        /// <returns></returns>
        protected string GetHref(object id, object albumID)
        {
            string url=string.Empty;
            if (albumID == null || albumID.ToString().Trim() == string.Empty)//说明为单张
            {
                url = ResolveClientUrl("~/SingleByPID.aspx?PID="+id.ToString());
            }
            else//组图
            {
                url = ResolveClientUrl("~/Album.aspx?id=" + albumID.ToString());
            }
            return url;
        }

        /// <summary>
        /// 页面第一次加载时显示图片列表中第一张图片的信息
        /// </summary>
        private void ShowFirstImageInfo(Photo photo)
        {
            lblUploadTime.Text = Convert.ToDateTime(photo.UploadTime).ToLongDateString();
            lblPhotoDescrip.Text = photo.Descrip;
            //根据图片ID取得用户及标签信息
            DataSet ds = (new PhotoBLL()).GetUserAndTagNameOfPhoto(photo.ID);
            DataRow dr = ds.Tables[0].Rows[0];
            lblLocation.Text = photo.Position;
            lblAuthor.Text = dr["UserName"].ToString();
            lblType.Text = dr["TagName"].ToString();
            //显示图片
            int width = 350;
            int height = 233;
            imagePreview.ImageUrl = ResolveClientUrl(PhotoBLL.GetThumbnailSrc(photo, width, height));
        }
    }
}